package com.zyd.blog.homeart.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.zyd.blog.framework.object.PageResult;
import com.zyd.blog.homeart.service.BackstageRotationChartService;
import com.zyd.blog.persistence.beans.*;
import com.zyd.blog.persistence.beans.management.Shop;
import com.zyd.blog.persistence.mapper.*;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BackstageRotationChartServiceImpl implements BackstageRotationChartService {
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private MyParticipateMapper myParticipateMapper;
    @Autowired
    private BackstageRotationChartMapper backstageRotationChartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private DesignArticleMapper designArticleMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CenterMapper centerMapper;
    @Autowired
    private CommentRelationMapper commentRelationMapper;
    @Autowired
    private ReportTypeMapper reportTypeMapper;
    @Autowired
    private DictionariesMapper dictionariesMapper;
    @Override
    public PageResult softBackstage(int page, int count) {
        PageResult pageResult = new PageResult();
        int i = backstageRotationChartMapper.selectCount(null);
        pageResult.setTotal((long)i);
        List<DesignArticle> softProducts = backstageRotationChartMapper.softBackstage(page * count - count, count);
        for (DesignArticle softProduct : softProducts) {
            if (softProduct.getDesignStyle().equals("12")) {
                softProduct.setDesignStyle(softProduct.getStyleContent());
            }
            if (softProduct.getDesignPlace().equals("12")) {
                softProduct.setDesignPlace(softProduct.getPlaceContent());
            }
            if (softProduct.getDesignApartment().equals("12")) {
                softProduct.setDesignApartment(softProduct.getApartmentContent());
            }
        }
        pageResult.setRows(softProducts);
        return pageResult;
    }

    @Override
    public Integer insertSoft(DesignArticle designArticle, String list) {
        List<Shop> shops = JSONArray.parseArray(list, Shop.class);
        //用作soft表的  imageId
        List<String> uuidList = new ArrayList<>();
        //用作soft表的  image  图片路径
        List<String> imageUrl = new ArrayList<>();

        designArticle.setDesignId(UuidUtils.getUUID());
        designArticle.setZanNumber(0);
        designArticle.setCollectionNumber(0);
        designArticle.setDiscussNumber(0);
        designArticle.setNow("1");
        designArticle.setAddTime(sdf.format(System.currentTimeMillis()));
        designArticle.setTopTime(sdf.format(System.currentTimeMillis()));
        designArticle.setNum(0);
        backstageRotationChartMapper.insert(designArticle);
        return null;
    }

    @Override
    public Integer editSoft(DesignArticle designArticle, String list, String id) {
        return null;
    }

//    @Override
//    public Integer editSoft(DesignArticle designArticle, String list, String id) {
//        String[] split1 = id.split(",,");
//        for (String s : split1) {
//            productMapper.deletePr(s);
//            reportTypeMapper.deleteRe(s);
//        }
//        List<Shop> shops = JSONArray.parseArray(list, Shop.class);
//        //用作soft表的  imageId
//        List<String> uuidList = new ArrayList<>();
//        //用作soft表的  image  图片路径
//        List<String> imageUrl = new ArrayList<>();
//        backstageRotationChartMapper.editSoftHave(softProduct.getSoftId(), softProduct.getSoftTitle(), softProduct.getSoftStyle(), softProduct.getSoftApartment(), softProduct.getSoftPlace(),
//                softProduct.getTop(), softProduct.getUseState(), softProduct.getCoverImage(), softProduct.getProvince(),
//                softProduct.getCity(), softProduct.getArea(), softProduct.getStyleContent(), softProduct.getApartmentContent(),
//                softProduct.getPlaceContent(), StringUtils.join(imageUrl, ",,"), StringUtils.join(uuidList, ",,"));
//
//        return 1;
//    }

    @Override
    public Integer deleteSoft(String softId) throws Exception {
            //根据文章id找到评论id  放到list
            List<CommentId> commentIds = commentMapper.commentAll(softId);
            for (CommentId commentId : commentIds) {
                try {
                    commentMapper.deleteComment(commentId.getCommentId());
                } catch (Exception e) {
                    throw new Exception("删除评论失败");
                }
                try {
                    commentRelationMapper.deleteCommentRelation(commentId.getCommentId());
                } catch (Exception e) {
                    throw new Exception("删除comment_relation失败");
                }
                try {
                    commentMapper.deleteCommentReply(commentId.getCommentId());
                } catch (Exception e) {
                    throw new Exception("删除评论回复失败");
                }
            }
            try {
                centerMapper.deleteCenter(softId);
            } catch (Exception e) {
                throw new Exception("删除center失败！");
            }
            try {
                myParticipateMapper.deleteArticle(softId);
            } catch (Exception e) {
                throw new Exception("从我的参与中删除失败");
            }

        try {
            backstageRotationChartMapper.deleteSoft(softId);

        } catch (Exception e) {
            throw new Exception("删除文章失败");
        }
        return 1;

    }
    @Override
    public Map<String, Object> searchDesign(String value, int page, int count) {
        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        int i = backstageRotationChartMapper.count(value);
        if (i%count==0){
            map.put("allPage",i/count);
        }else {
            map.put("allPage",i/count+1);
        }
        map.put("list",backstageRotationChartMapper.searchDesign(value,page*count-count,count));
        return map;
    }

    @Override
    public Map<String, Object> specific(String designId) throws Exception {
        Map<String,Object> map = new HashMap<>();
        try {
            DesignArticle specific = backstageRotationChartMapper.get(designId);
            specific.setDesignStyle(dictionariesMapper.content(specific.getDesignStyle()));
            specific.setDesignApartment(dictionariesMapper.content(specific.getDesignApartment()));
            specific.setDesignPlace(dictionariesMapper.content(specific.getDesignPlace()));
            map.put("specific",specific);
        } catch (Exception e) {
            throw new Exception("获取内容失败");
        }
        return map;
    }

    @Override
    public DesignArticle get(String id) {
        return backstageRotationChartMapper.get(id);
    }

    @Override
    public Integer updateIn(DesignArticle designArticle) {
        return backstageRotationChartMapper.updateIn(designArticle.getDesignId(),designArticle.getUseState());
    }

    @Override
    public Integer success(String id) {
        return backstageRotationChartMapper.success(id);
    }

    @Override
    public Integer lose(String id, String loseContent) {
        return backstageRotationChartMapper.lose(id,loseContent);
    }

    @Override
    public Integer hide(String id, String commentId, String content) {
        //此篇文章  评论数量-1
        Integer integer=0;
        Integer hide=0;
        if (content.equals("隐藏")){
            designArticleMapper.commentReduce(id);

            hide = commentMapper.hide(commentId);
        }
        if (content.equals("不隐藏")){
            designArticleMapper.commentNum(id);
            integer = commentMapper.noHide(commentId);
        }
        return hide+integer;
    }
}
